package com.example.his.api.common;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.his.api.exception.HisException;
import lombok.extern.slf4j.Slf4j;
//import com.ctc.wstx.util.URLUtil;
//import org.apache.http.HttpEntity;
//import org.apache.http.client.methods.HttpRequestBase;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//import com.cloud.apigateway.sdk.utils.Request;
//import com.cloud.apigateway.sdk.utils.Client;

import java.util.HashMap;

@Component
@Slf4j
public class OcrUtil {
  @Value("${huawei.appKey}")
  private String appKey;
  
  @Value("${huawei.appSecret}")
  private String appSecret;
  
  public HashMap identifyWaybill(String imageBase64) throws Exception {
    //    Request request = new Request();
    //    request.setKey(appKey);
    //    request.setSecret(appSecret);
    //    request.setMethod("POST");
    //    request.setUrl("https://jemxpressbill.apostore.huaweicloud.com/orc/express-bill");
    //    request.addHeader("Content-Type", "application/x-www-form-urlencoded");
    //    //    对上传的图片base64字符串做url编码
    //    String encode = URLUtil.encodeAll(imageBase64);
    //    //    在请求体中设置上传的图片
    //    request.setBody("image=" + encode);
    //    //    对请求体中的内容生成数字签名
    //    HttpRequestBase signenRequest = Client.sign(request);
    //    //    创建http客户端的对象
    //    CloseablehttpClient client = HttpClients.custom().build();
    //    //    发出http请求，得到返回的相应
    //    CloseableHttpResponse response = client.execute(signenRequest);
    //    if(response.getStatusLine().getStatusCode() != 200) {
    //      log.error("OCR请求失败", response.toString());
    //      throw new HisException("OCR请求失败,运单识别失败");
    //    }
    //    HttpEntity entity = response.getEntity();
    HashMap entity = new HashMap() {{
      put("recName", "吴某");
      put("recTel", "13871999999");
      put("waybillCode", "AEADAS13871999999");
      put("code", 200);
      put("msg", "成功");
      
    }};
    if(entity != null) {
      //      String string = EntityUtils.toString(entity, "UTF-8");
      String string = entity.toString();
      JSONObject json = JSONUtil.parseObj(string);
      int code = json.getInt("code");
      String msg = json.getStr("msg");
      if(code == 200) {
        JSONObject data = json.getJSONObject("data");
        //        收件人姓名
        String recName = data.getStr("recipient_name");
        //        收件人电话
        String recTel = data.getStr("recipient_phone");
        //        快递运单号
        String waybillCode = data.getStr("waybill_number");
        return new HashMap() {{
          put("recName", recName);
          put("recTel", recTel);
          put("waybillCode", waybillCode);
        }};
      } else {
        log.error("OCR请求失败", msg);
        throw new HisException("OCR请求失败,运单识别失败");
      }
    } else {
      throw new HisException("OCR请求失败,请求体为空");
    }
  }
}
